import Login from '@/pages/Login'
import Register from '@/pages/Register'
import Detail from '@/pages/Detail'
import AddCartSuccess from '@/pages/AddCartSuccess'
import ShopCart from '@/pages/ShopCart'
import Trade from '@/pages/Trade'
import Pay from '@/pages/Pay'
import PaySuccess from '@/pages/PaySuccess'
import Center from '@/pages/Center'
// import MyOrder from '@/pages/Center/myOrder'
import GroupOrder from '@/pages/Center/groupOrder'
export default [
  // 路由重定向
  { path: '/', redirect: '/home' },
  {
    path: '/home',
    component: () => import('@/pages/Home'),
    meta: { show: true }
  },
  {
    // ? 代表可传可不传
    path: '/search/:keyWord?',
    component: () => import('@/pages/Search'),
    meta: { show: true },
    name: 'search'
  },
  { path: '/login', component: Login },
  { path: '/register', component: Register },
  { path: '/detail/:skuid', component: Detail, meta: { show: true } },
  {
    path: '/addcartsuccess',
    component: AddCartSuccess,
    name: 'addcartsuccess',
    beforeEnter(to, from, next) {
      // 得到当前路由信息对象
      // const route = router.currentRoute  // route就是from
      // 得到要跳转到目路由的query参数
      const skuNum = to.query.skuNum
      // 读取保存的数据
      const skuInfo = JSON.parse(window.sessionStorage.getItem('SKUINFO'))
      console.log('---', skuNum, skuInfo.spuId)
      // 只有都存在, 才放行
      if (skuNum && skuInfo) {
        next()
      } else {
        // 在组件对象创建前强制跳转到首页
        next('/')
      }
    }
  },
  {
    path: '/shopCart',
    component: ShopCart
  },
  {
    path: '/trade',
    component: Trade,
    /* 只能从购物车界面, 才能跳转到交易界面 */
    beforeEnter(to, from, next) {
      if (from.path === '/shopcart') {
        next()
      } else {
        next('/shopcart')
      }
    }
  },
  {
    path: '/pay',
    component: Pay,
    /* 只能从交易界面, 才能跳转到支付界面 */
    beforeEnter(to, from, next) {
      if (from.path === '/trade') {
        next()
      } else {
        next('/trade')
      }
    }
  },
  {
    path: '/paysuccess',
    component: PaySuccess
  },
  {
    path: '/center',
    redirect: '/center/myorder',
    component: Center,
    children: [
      { path: 'myorder', component: ()=>import('@/pages/Center/myOrder') },
      { path: 'grouporder', component: GroupOrder }
    ]
  }
]
